<template>
	<view class="bankcard-public-box">
		<view class="bankcard-top-box">
			<text>账户类型</text>
			<text class="bankcard-top-text-value">对公</text>
		</view>
		<view class="bankcard-title-box">请上传开户许可证或者基本存款账户信息照片</view>
		<!-- 许可证照片 -->
		<view class="bankcard-image-box">
			<image class="bankcard-image" :src="formStore.bankFrontPhoto || '/static/xkz_img.png'" mode="scaleToFill"></image>
		</view>
		<view class="bankcard-title-box">请核对信息，若有误请手动更正</view>
		<view class="bankcard-form">
			<xh-form-input title="银卡卡号" placeholder="请输入对公银行账号" v-model="formStore.bankCardNo"></xh-form-input>
			<xh-form-input title="开户银行" placeholder="请选择" isSelect @showSelect="showSelectBank" v-model="formStore.branchBankName"></xh-form-input>
			<xh-form-input title="开户城市" placeholder="省/市" isSelect @showSelect="showSelectCity" v-model="selectCityValue"></xh-form-input>
			<xh-form-input title="开户支行" placeholder="请选择" isSelect @showSelect="showSelectSubBank" v-model="formStore.bankName"></xh-form-input>
		</view>
		<!-- 底部提交按钮组件 -->
		<xh-submit title="提交" @btnClick="submitClick"></xh-submit>
		<xh-select-city ref="selectCityRef"></xh-select-city>
	</view>
</template>

<script setup>
	
	import { ref, computed } from 'vue'
	import { useFormStore } from '@/stores'
	//城市选择组件ref对象
	const selectCityRef = ref()
	const formStore = useFormStore()
	
	const selectCityValue = computed(() => {
		if (!uni.$u.test.isEmpty(formStore.cityCode)) {
			return `${formStore.provinceName},${formStore.cityName}`
		}
		return ''
	})
	
	
	//选择开户银行
	const showSelectBank = () => {
		uni.navigateTo({
			url: '/pages/auth/select-bank/select-bank'
		})
	}
	//选择开户城市
	const showSelectCity = ()=>{
		selectCityRef.value.open()
	}
	//选择开户支行
	const showSelectSubBank = ()=>{
		if (uni.$u.test.isEmpty(formStore.branchBankName)) {
			uni.$tools.showToast('请选择开户银行')
			return
		} else if (uni.$u.test.isEmpty(formStore.provinceCode)) {
			uni.$tools.showToast('请选择开户城市')
			return
		}
		uni.navigateTo({
			url: '/pages/auth/select-sub-branch/select-sub-branch'
		})
	}
	///提交
	const submitClick = ()=>{
		if (uni.$u.test.isEmpty(formStore.bankFrontPhoto)) {
			uni.$tools.showToast('请上传开户许可证或者基本存款账户信息照片')
			return
		} else if (uni.$u.test.isEmpty(uni.$tools.clearSpace(formStore.bankCardNo))) {
			uni.$tools.showToast('请输入对公银行卡号')
			return
		} else if (uni.$u.test.isEmpty(formStore.branchBankName)) {
			uni.$tools.showToast('请选择开户银行')
			return
		} else if (uni.$u.test.isEmpty(formStore.provinceCode)) {
			uni.$tools.showToast('请选择开户城市')
			return
		} else if (uni.$u.test.isEmpty(formStore.bankName)) {
			uni.$tools.showToast('请选择开户支行')
			return
		} 
		uni.$pop.show({
			type:'alert',
			config:{
				title:'温馨提示',
				desc:'确认要提交审核吗？',
				buttons:['取消','确认']
			}
		},(value)=>{
			if(value == '确认'){
				uni.$u.throttle(submitMerchantInfoAction, 500)
			}
		})
	}
	//确认提交企业商户信息审核
	const submitMerchantInfoAction = ()=> {
		let params = {}
		//法人身份信息
		params.userRealName = formStore.userRealName.trim()
		params.identityNo = formStore.identityNo.trim()
		let certValidTime = ''
		if(formStore.isLongTime){
			certValidTime = `${formStore.certStartTime},长期`
		}
		else{
			certValidTime = `${formStore.certStartTime},${formStore.certEndTime}`
		}
		params.certValidTime = certValidTime
		params.frontPhoto = formStore.frontPhoto
		params.reversePhoto = formStore.reversePhoto
		params.handsetPhoto = formStore.handsetPhoto
		if(!uni.$u.test.isEmpty(formStore.sex)){
			params.sex = formStore.sex
		}
		if(!uni.$u.test.isEmpty(formStore.nation)){
			params.nation = formStore.nation
		}
		params.address = uni.$tools.clearSpaceAndEnter(formStore.address)
		
		//结算卡信息
		params.settleType = '0'
		params.bankFrontPhoto = formStore.bankFrontPhoto
		params.bankCardNo = uni.$tools.clearSpace(formStore.bankCardNo)
		params.bankNo = formStore.bankNo
		params.bankName = formStore.bankName
		if(!uni.$u.test.isEmpty(formStore.bankBranchNo)){
			params.bankBranchNo = formStore.bankBranchNo
		}
		params.branchBankName = formStore.branchBankName
		params.bankPhone = uni.$tools.clearSpace(formStore.bankPhone)
		let bankAddress = ''
		if(!uni.$u.test.isEmpty(formStore.provinceCode) && uni.$u.test.isEmpty(formStore.cityCode)){
			bankAddress = formStore.provinceCode
		}
		else if(!uni.$u.test.isEmpty(formStore.provinceCode) && !uni.$u.test.isEmpty(formStore.cityCode)){
			bankAddress = `${formStore.provinceCode},${formStore.cityCode}`
		}
		params.bankAddress = bankAddress
		//店铺信息
		params.mercSn = formStore.mercSn.trim()
		params.mccType = formStore.mccType
		params.bussinessAddr = uni.$tools.clearSpaceAndEnter(formStore.bussinessAddr)
		if(!uni.$u.test.isEmpty(formStore.settleId)){
			params.settleId = formStore.settleId
		}
		params.merType = formStore.merType
		const bussinessArea = `${formStore.storeProvinceName},${formStore.storeCityName},${formStore.storeAreaName}`
		params.bussinessArea = bussinessArea
		
		///企业信息
		params.busLicenseInfo = form.busLicenseInfo
		params.busRegType = form.busRegType
		params.busLicenseId = form.busLicenseId
		params.doorPhoto = form.doorPhoto
		params.indoorPhoto = form.indoorPhoto
		params.cashierPhoto = form.cashierPhoto
		params.companyName = form.companyName
		if(formStore.busRegType == '1'){
			params.busRegStartDt = form.busRegStartDt
			params.busRegEndDt = form.busRegEndDt
		}
		else{
			params.busRegStartDt = form.busRegStartDt
			params.busRegEndDt = '2999-12-30'
		}
		//提交商户信息
		submitMerchantInfo(params).then(res=>{
			submitMerchantReportAction()
		}).catch(()=>{
		})
	}
	//提交商户报备
	const submitMerchantReportAction = ()=>{
		submitMerchantReport({"uptType":"4"}).then(res=>{
			showResultPage('submit')
		}).catch(err=>{
			if(!uni.$u.test.isEmpty(err.message)){
				userStore.user.authRemark = err.message.content
			}
			else{
				userStore.user.authRemark = '提交失败，请检查网络'
			}
			showResultPage('fail')
		})
	}
	//跳转结果页面
	const showResultPage = (type)=>{
		uni.switchTab({
			url:'/pages/my/my',
			complete() {
				formStore.$reset()
				uni.navigateTo({
					url:`/pages/result/result?type=${type}`
				})
			}
		})
	}
</script>

<style lang="scss" scoped>
.bankcard-public-box{
	font-size: 28rpx;
	.bankcard-top-box{
		display: flex;
		align-items: center;
		padding: 30rpx;
		background-color: white;
		.bankcard-top-text-value{
			margin-left: 40rpx;
			color: $xh-subtitle-color;
		}
	}
	.bankcard-title-box{
		padding: 26rpx 30rpx;
		font-weight: bold;
		color: $xh-subtitle-color;
	}
	.bankcard-image-box{
		background-color: white;
		padding: 30rpx 60rpx;
		.bankcard-image{
			width: 630rpx;
			height: 270rpx;
		}
	}
}
</style>
